package com.originskyseed.platform.count.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.count.mapper.UserBonusMapper;
import com.originskyseed.platform.count.model.UserBonus;
import com.originskyseed.platform.count.service.IUserBonusService;

/**
 * 用户奖金信息表Service接口实现类（OSI_USER_BONUS）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("userBonusService")
public class UserBonusServiceImpl implements IUserBonusService {

	@Resource
	private UserBonusMapper userBonusMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.userBonusMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.userBonusMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(UserBonus record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userBonusMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(UserBonus record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userBonusMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(UserBonus record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userBonusMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(UserBonus record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userBonusMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserBonus selectByPrimaryKey(String id) throws Exception {
		return this.userBonusMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserBonus> selectList(UserBonus record) throws Exception {
		return this.userBonusMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserBonus> selectListByPage(Map<String, Object> params, BasePage<UserBonus> page) throws Exception {
		params.put("page", page);
		List<UserBonus> list = this.userBonusMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 统计季度、年度奖金
	 * 
	 * @param yearStartTime
	 * @param yearEndTime
	 * @param monthStartTime
	 * @param monthEndTime
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	public int insertsUserBonusByTime(String yearStartTime, String yearEndTime, String monthStartTime, String monthEndTime, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		insertsUserBonusByTime(yearStartTime, yearEndTime, 1);
		insertsUserBonusByTime(monthStartTime, monthEndTime, 0);
		return 1;
	}

	/**
	 * 统计季度年度奖金
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public int insertsUserBonusByTime(String startTime, String endTime, Integer type) throws Exception {
		UserBonus query = new UserBonus();
		query.setStartTime(startTime);
		query.setEndTime(endTime);
		List<UserBonus> userBonuses = this.userBonusMapper.selectList(query);
		if (userBonuses.size() > 0) {
			return 0;
		}
		List<UserBonus> list = this.userBonusMapper.selectUserBonusByTime(startTime, endTime);
		startTime = startTime.substring(0, 7);
		endTime = endTime.substring(0, 7);
		for (UserBonus userBonus : list) {
			userBonus.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			userBonus.setStartTime(startTime);
			userBonus.setEndTime(endTime);
			userBonus.setStatus(0);
			userBonus.setType(type);
			userBonus.setCreateTime(new Date());

			// 优质奖金
			if (userBonus.getHighGradeCount() != null && userBonus.getQualityBonusBase() != null) {
				userBonus.setHighGradeBonus(new BigDecimal(userBonus.getHighGradeCount()).multiply(userBonus.getQualityBonusBase()));
			}
		}

		int count = 0;
		// 一次性插入的数量
		int size = 500;
		// 总长度
		int length = list.size();
		// 计算可以分成多少组
		int num = (length + size - 1) / size;
		for (int i = 0; i < num; i++) {
			// 开始位置
			int fromIndex = i * size;
			// 结束位置
			int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
			List<UserBonus> newUserBonus = list.subList(fromIndex, toIndex);
			count += userBonusMapper.insertBatch(newUserBonus);
		}
		return count;
	}

}